package com.example.myapplication2048.itemprovider;

import com.example.myapplication2048.ResourceTable;
import com.example.myapplication2048.datamodel.Textinfo;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.agp.components.RecycleItemProvider;

import java.util.List;

public class TextProvider extends RecycleItemProvider {
    private List<Textinfo> items;
    public TextProvider(List<Textinfo> dataItemList) {

        this.items = dataItemList;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component itemComponent = component;
        TextHolder dynamicitem;
        if(itemComponent == null){
            Context context = componentContainer.getContext();


            itemComponent = LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_textdemo, componentContainer, false);
            dynamicitem =  new TextHolder(itemComponent);
            itemComponent.setTag(dynamicitem);
        } else {
            dynamicitem = (TextHolder) itemComponent.getTag();
        }
        System.out.println("123"+items.get(i).getAuthor());
        dynamicitem.authname2.setText(items.get(i).getAuthor());
        return itemComponent;
    }

    public static   class TextHolder {
        Text authname2;
        TextHolder(Component itemComponent){
            authname2 = (Text) itemComponent.findComponentById(ResourceTable.Id_authname2);
        }
    }
}
